<template>
    <!-- pages/signDetail/index.wxml -->
    <view class="page">
		<cu-custom bgColor="bg-gray" :isBack="true">
			<block slot="right">
				<view class="headShareBox margin-right-xs">
					<view class="itemCon flex align-center" v-if="isEdit" @tap="showSheet">
						<image src="../../static/xiu.png" mode=""></image>
						<text>修改</text>
					</view>
					<view class="itemCon flex align-center"  v-if="!isEdit"  @tap="showSheet2" >
						<image src="../../static/sz.png" mode=""></image>
						<text>设置</text>
					</view>
					<view class="itemCon flex align-center" @tap="shareMeeting">
						<image src="../../static/share.png" mode=""></image>
						<text>分享</text>
					</view>
				</view>
			</block>
		</cu-custom>
        <view class="title_background">
            <text class="ckqd_title">{{ meeting.title }}</text>
            <text class="ckqd_title_time">{{ signName }}</text>
            <text class="ckqd_title_time">{{ signTime }}</text>
        </view>
        <view class="qhcon">
            <view class="tabs">
                <view :class="(isEdit ? 'tabs-item' : 'tabs-item2') + ' ' + (currentTab == 0 ? 'active' : '')" data-current="0" @tap="clickTab">签到</view>
                <view :class="(isEdit ? 'tabs-item' : 'tabs-item2') + ' ' + (currentTab == 1 ? 'active' : '')" data-current="1" @tap="clickTab">简介</view>
                <view :class="(isEdit ? 'tabs-item' : 'tabs-item2') + ' ' + (currentTab == 2 ? 'active' : '')" data-current="2" @tap="clickTab">评论</view>
                <view class="tabs-item" v-if="isEdit" :data-url="'/pages/signData/signData?meetingId=' + meeting.id" @tap="navigator">数据</view>
            </view>
            <view class="tab-content">
                <view :class="'tab-list-content ' + (currentTab == 0 ? 'active' : '')">
                    <my-sign :isEdit="isEdit" :eventList="meeting.meetingEventList"></my-sign>
                </view>
                <view :class="'tab-list-content ' + (currentTab == 1 ? 'active' : '')">
                    <my-info :coverImg="meeting.coverImg" :description="meeting.description"></my-info>
                </view>
                <view :class="'tab-list-content ' + (currentTab == 2 ? 'active' : '')">
                    <my-comment v-if="meeting.comment == 1" :meetingId="meeting.id"></my-comment>
                    <view class="errtipsk" v-if="meeting.comment == 0">
                        <text class="errtips">不允许评论</text>
                    </view>
                </view>
            </view>
        </view>
        <!-- <share :actionShow="showStatus" closeText="关闭" @actionHide="onActionHide" :meetingCode="meeting.code"></share> -->
    </view>
</template>

<script>
import mySign from './sign/sign';
import myInfo from './info/info';
import myComment from './comment/comment';

// pages/signDetail/index.js
import api from '../../utils/api';
const app = getApp();
export default {
    components: {
        mySign,
        myInfo,
        myComment,
        // share
    },
    data() {
        return {
            currentTab: 0,

            meeting: {
                title: '',
                id: '',
                meetingEventList: '',
                coverImg: '',
                description: '',
                comment: 0,
                code: ''
            },

            id: null,
            isEdit: false,
            sginName: '',
            signTime: '',
            showStatus: false,
            activeTab: '',
            signName: '',
            removeJoinId: '',
            removeCreateId: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var id = options.id;
        this.requestInfo(id);
        this.id = id
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let id = this.id;

        if (this.isEdit == true && id) {
            this.requestInfo(id);
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        onChange(e) {
            const index = e.detail.index;

            if (index == 3) {
                return;
            }
			this.activeTab = index;
        },

        navigator(e) {
            let url = e.currentTarget.dataset['url'];
            uni.navigateTo({
                url: url
            });
        },

        requestInfo(id) {
            let that = this;
            api.get('/rest/api/meetingDetail', {
                id: id
            }).then((res) => {
                console.log(res);
                let currEvent = that.findEvent(res.data.meeting.meetingEventList);

                if (currEvent == null) {
                    currEvent = {
                        title: '',
                        startTime: ''
                    };
                }

				this.meeting = res.data.meeting,
				this.isEdit=res.data.isEdit,
				this.signName=currEvent.title,
				this.signTime=currEvent.startTime
            });
        },

        showSheet(e) {
            var id = e.currentTarget.dataset['id'];
            var that = this;
            uni.showActionSheet({
                itemList: ['修改', '删除'],

                success(res) {
                    if (res.tapIndex == 0) {
                        //修改
                        uni.navigateTo({
                            url: '/pages/meetingCreate/meetingCreate?id=' + id
                        });
                    } else if (res.tapIndex == 1) {
                        //删除
                        uni.showModal({
                            title: '提示',
                            content: '确认要删除该房间吗？',

                            success(res) {
                                if (res.confirm) {
                                    that.delMeeting(id);
                                }
                            }
                        });
                    }
                }
            });
        },

        showSheet2(e) {
            var id = e.currentTarget.dataset['id'];
            uni.showActionSheet({
                itemList: ['离开房间'],

                success(res) {
                    if (res.tapIndex == 0) {
                        uni.showModal({
                            title: '提示',
                            content: '确认要离开该房间吗？',

                            success(res) {
                                if (res.confirm) {
                                    uni.showLoading({
                                        title: '提交中...'
                                    });
                                    uni.request({
                                        url: getApp().globalData.serverUrl + '/rest/api/exitMeeting',
                                        data: {
                                            id: id
                                        },
                                        method: 'POST',
                                        header: {
                                            'content-type': 'application/x-www-form-urlencoded',
                                            sessionId: getApp().globalData.sessionId
                                        },
                                        //请求成功，回调函数
                                        success: function (res) {
                                            var resp = res.data;
                                            console.log(resp); //隐藏加载提示

                                            uni.hideLoading();

                                            if (resp.code != 0) {
                                                uni.showToast({
                                                    title: resp.msg || '未知异常',
                                                    icon: 'error',
                                                    duration: 1200
                                                });
                                                return;
                                            }

                                            uni.showToast({
                                                title: '退出成功',
                                                icon: 'success',
                                                duration: 1000,
                                                success: function () {
                                                    var pages = getCurrentPages();
                                                    var prevPage = pages[pages.length - 2];
                                                    prevPage.setData({
                                                        removeJoinId: id
                                                    });
                                                    uni.navigateBack({
                                                        delta: 1
                                                    });
                                                }
                                            });
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            });
        },

        shareMeeting(e) {
			this.showStatus = true
        },

        onActionHide: function () {
            console.log('ActionSheet关闭了');
            this.showStatus = false
        },

        delMeeting(id) {
            api.post('/rest/api/delMeeting', {
                id: id
            }).then((res) => {
                app.globalData.successTip('删除成功');
                setTimeout(function () {
                    var pages = getCurrentPages();
                    var prevPage = pages[pages.length - 2];
                    prevPage.setData({
                        removeCreateId: id
                    });
                    uni.navigateBack({
                        delta: 1
                    });
                }, 1000);
            });
        },

        findEvent(eventList) {
            //首先查找执行中的
            let result = null;
            eventList.some((item, index) => {
                if (index == 0 && item.status == 0) {
                    result = item;
                    return true;
                }

                if (item.status == 1) {
                    result = item;
                    return true;
                }
            });

            if (result == null) {
                //如果没有进行中的事件，则找最后一个结束事件后面的事件
                var lastEndIndex = -1;
                eventList.some((item, index) => {
                    if (item.status == 2) {
                        lastEndIndex = index;
                    }
                });

                if (lastEndIndex < eventList.length) {
                    result = eventList[lastEndIndex];
                }
            }

            return result;
        },

        clickTab: function (e) {
            var current = e.currentTarget.dataset.current;
            this.currentTab = parseInt(current)
        }
    }
};
</script>
<style lang="scss">
/* pages/signDetail/index.wxss */
.page{
	padding: 20upx 32upx;
}
.headShareBox{
	width: 264upx;
	height: 60upx;
	background: #FFFFFF;
	border-radius: 30upx;
	padding: 0 32upx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	.itemCon{
		font-size: 28upx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
		image{
			width: 24upx;
			height: 24upx;
			margin-right: 8upx;
		}
	}
}
scroll-view {
    margin: 10rpx 20rpx;
    width: auto !important;
    background: transparent !important;
}

.title_background {
    color: white;
    background-color: rgba(27, 117, 255, 1);
    border-radius: 20rpx;
    text-align: center;
    margin-top: 0;
    padding: 60rpx 30rpx;
}

.ckqd_title {
    display: block;
    color: rgba(255, 255, 255, 1);
    font-size: 48rpx;
    white-space: nowrap;
    line-height: 66rpx;
    margin-bottom: 20rpx;
}

.ckqd_title_time {
    display: block;
    color: rgba(255, 255, 255, 1);
    font-size: 32rpx;
    text-align: center;
    white-space: nowrap;
    line-height: 44rpx;
}

.caozuok {
    text-align: center;
}

.errtips {
    text-align: center;
    color: #666;
    display: inline;
}
.errtipsk {
    text-align: center;
    padding: 100rpx 0;
}
.button_1 {
    background-color: rgba(255, 255, 255, 1);
    border-radius: 30rpx;
    width: auto !important;
    padding: 6rpx 28rpx 6rpx 28rpx !important;
    float: right !important;
    font-size: 28rpx !important;
    margin-left: 20rpx !important;
    margin-right: 0 !important;
    display: inline;
}

.tabs {
    width: 100%;
    position: absolute;
    left: 0;
    right: 0;
	height: 80upx;
	background: #FFFFFF;
	border-radius: 20upx;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.qhcon {
    position: relative;
	margin-top: 24upx;
}

.tabs-item {
    width: 25%;
    display: inline-block;
	position: relative;
    text-align: center;
    height: 66rpx;
	font-size: 28upx;
    line-height: 66rpx;
    font-weight: bold;
	&.active {
		color: #1A75FF;
		font-size: 32upx;
		&::after{
			content: "";
			width: 60upx;
			height: 6upx;
			background: #1A75FF;
			position: absolute;
			bottom: -6upx;
			left: 33%;
			border-radius: 20upx;
		}
	}

}
.tab-content {
    padding-top: 120rpx;
    overflow: scroll;
}

.tab-list-content {
    display: none;
    height: 100%;
}

.tab-list-content.active {
    display: block;
}

.text {
    background-attachment: white;
}
</style>
